package com.yamanyar.weebill.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

import org.hibernate.validator.NotNull;

//TODO ADD LISTENER-ABLILITY
/**
 * 
 * <code>ActionPathElement</code> object is an element of
 * <code>ActionPath</code> object. In order to understand use of
 * <code>ActionPathElement</code> please check the javadoc of
 * <code>Action</code>
 * 
 * @see Action.
 * 
 * @author Kaan Yamanyar
 * 
 */
@Entity
public class ActionPathElement implements Comparable<ActionPathElement> {
	private Long id;

	private Integer order;

	private ActionPath actionPath;

	private Action action;

	private List<Role> roles = new ArrayList<Role>();

	@ManyToOne
	@JoinColumn(name = "actionPathId")
	// actionPathId will be created in ActionPathElement object's table
	@NotNull
	public ActionPath getActionPath() {
		return this.actionPath;
	}

	public void setActionPath(ActionPath actionPath) {
		this.actionPath = actionPath;
	}

	@Id
	@GeneratedValue
	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "sequenceNo")
	public Integer getOrder() {
		return this.order;
	}

	public void setOrder(Integer order) {
		this.order = order;
	}

	public int compareTo(ActionPathElement that) {
		if (this.getOrder() == null) {
			return -1;
		}
		if (that == null) {
			return 1;
		}
		int o = this.getOrder().compareTo(that.getOrder());
		return o;
	}

	@Override
	public boolean equals(Object object) {
		if (this.getId() == null) {
			return false;
		}
		if ((object == null) || !(object instanceof ActionPathElement)) {
			return false;
		}
		return this.getId().equals(((ActionPathElement) object).getId());
	}

	@Override
	public int hashCode() {
		if (id == null)
			return 300;
		return id.hashCode();
	}

	@ManyToOne
	@JoinColumn(name = "actionId")
	// actionId will be created in ActionPathElement object's table
	@NotNull
	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	@ManyToMany(targetEntity = Role.class, fetch = FetchType.LAZY)
	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public void addRole(Role role) {
		roles.add(role);
	}
}
